<template>
  <md-main class="md-web-product">
    <el-collapse-transition>
      <div class="md-body mb20" v-show="showSearch">
        <el-form :model="search" ref="search" size="small" :inline="true">
          <el-form-item label="导航名称" prop="name">
            <el-input v-model="search.name" class="w150" placeholder="请输入" clearable />
          </el-form-item>
          <el-form-item label="是否外链" prop="isExternalLink">
            <el-select v-model="search.isExternalLink">
              <el-option value="0" label="是" />
              <el-option value="1" label="否" />
            </el-select>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <md-dict-select :options="dictData['sys_normal_disable']" v-model="search.status" placeholder="请选择" class="w150"></md-dict-select>
          </el-form-item>
          <el-form-item>
            <md-button type="primary" icon="el-icon-search" :prems="['web:navigation:query']" @click="handleQuery()">搜索</md-button>
            <md-button icon="el-icon-refresh" @click="handleQueryReset()">重置</md-button>
          </el-form-item>
        </el-form>
      </div>
    </el-collapse-transition>

    <div class="md-body md-table">
      <md-toolbar>
        <md-button type="primary" size="small" plain :prems="['web:navigation:add']" @click="handleAdd()">新增</md-button>
        <md-button type="success" size="small" plain :prems="['web:navigation:remove']" :disabled="tableSelectIds.length===0" @click="handleDelete()">批量删除</md-button>

        <template slot="right">
          <md-button size="mini" icon="el-icon-search" circle @click="showSearch=!showSearch"></md-button>
          <md-button type="primary" size="mini" icon="el-icon-refresh" circle @click="getTableDataList()"></md-button>
        </template>
      </md-toolbar>

      <el-table v-loading="tableLoading" :data="tableData" @selection-change="handleTableSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="编号" align="center" prop="navigationId" width="100" />
        <el-table-column label="导航名称" align="left" prop="name" min-width="200" show-overflow-tooltip />
        <el-table-column label="链接地址" align="left" prop="pagePath" width="200" show-overflow-tooltip>
          <template slot-scope="{ row }">
            {{row.isExternalLink==='0'? row.link: row.pagePath}}
          </template>
        </el-table-column>
        <el-table-column label="排序" align="center" prop="sort" width="100" />
        <el-table-column label="状态" align="center" prop="status" width="100">
          <template slot-scope="{ row }">
            <md-dict-tag :options="dictData['sys_normal_disable']" :props="{ label: 'dictLabel', value: 'dictValue' }" :value="row.status" />
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
        <el-table-column label="操作" align="center" fixed="right" width="150">
          <template slot-scope="{ row }">
            <md-button size="mini" type="text" icon="el-icon-edit" :prems="['web:navigation:edit']" @click="handleUpdate(row)">修改</md-button>
            <md-button size="mini" type="text" icon="el-icon-delete" :prems="['web:navigation:remove']" @click="handleDelete(row)">删除</md-button>
          </template>
        </el-table-column>
      </el-table>

      <md-pagination v-show="tableTotal>0" :total="tableTotal" :page.sync="search.pageIndex" :limit.sync="search.pageSize" @pagination="getTableDataList" />
    </div>

    <!-- 添加或修改对话框 -->
    <el-dialog :title="dialog.title" :visible.sync="dialog.open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="request" :rules="rules" size="small" label-width="80px" label-position="right">
        <el-form-item label="导航名称" prop="name">
          <el-input v-model="request.name" placeholder="请输入导航名称" />
        </el-form-item>

        <el-form-item label="是否外链" prop="isExternalLink">
          <el-radio-group v-model="request.isExternalLink">
            <el-radio label="0">是</el-radio>
            <el-radio label="1">否</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="页面地址" prop="pagePath" v-if="request.isExternalLink==='1'">
          <el-input v-model="request.pagePath" placeholder="请输入页面地址" />
        </el-form-item>

        <el-form-item label="外链地址" prop="link" v-if="request.isExternalLink==='0'">
          <el-input v-model="request.link" placeholder="请输入外链地址" />
        </el-form-item>

        <el-form-item label="排序" prop="sort">
          <el-input type="number" v-model="request.sort" placeholder="请输入排序" />
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <md-dict-radio-group :options="dictData['sys_normal_disable']" v-model="request.status"></md-dict-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" @click="handleSubmit">确 定</el-button>
        <el-button size="small" @click="handleCancel">取 消</el-button>
      </div>
    </el-dialog>
  </md-main>
</template>

<script>
import { listPageNavigation, insertNavigation, updateNavigation, delNavigation, getNavigation } from '@/api/web/navigation.service.js'
import { mdGetDictData } from '@/ddd/dict.service.js'
export default {
  name: 'MdWebProductComponent',
  data () {
    return {
      // 显示隐藏查询条件
      showSearch: true,
      // 查询参数
      search: { ...searchParams },
      // 列表加载
      tableLoading: false,
      // 列表数据
      tableData: [],
      // 列表数据，总条数
      tableTotal: 0,
      // 已选择的列表数据
      tableSelectIds: [],
      // 添加/编辑请求参数
      request: requestParams,
      rules: rules,
      // 添加/编辑弹窗
      dialog: {
        title: '添加导航',
        open: false
      },
      dictData: {}
    }
  },
  mounted () {
    mdGetDictData(['sys_normal_disable', 'sys_yes_no']).then(res => {
      this.dictData = res
    })
    this.getTableDataList()
  },
  methods: {
    /** 获取数据 */
    async getTableDataList () {
      this.tableLoading = true
      try {
        const res = await listPageNavigation(this.search)

        const data = res.data.items

        this.tableData = data
        this.tableTotal = res.data.totalItems
      } catch (error) {
        console.log(error)
      }

      this.tableLoading = false
    },
    /** 表格复选框事件 */
    handleTableSelectionChange (event) {
      this.tableSelectIds = event.map(m => { return m.navigationId })
    },
    /** 搜索 */
    handleQuery () {
      this.search.pageNum = 1

      this.getTableDataList()
    },
    /** 重置搜索 */
    handleQueryReset () {
      this.search = { ...searchParams }

      this.getTableDataList()
    },
    // 重置表单数据
    handleReset () {
      this.request = { ...requestParams }
      if (this.$refs.form) {
        this.$refs.form.resetFields();
      }
    },
    /** 删除 */
    async handleDelete (row) {
      try {
        const ids = row?.navigationId || this.tableSelectIds

        await this.$confirm(`是否确认删除编号为${ids}的数据项？`)

        await delNavigation(ids)

        this.$message.success('删除成功')

        this.getTableDataList()
      } catch (error) {
        console.log(error)
      }
    },
    /** 添加 */
    async handleAdd () {
      try {
        this.handleReset()
        this.dialog.title = '添加导航'

        this.dialog.open = true

        this.request = { ...requestParams }
      } catch (error) {
        console.log(error)
      }
    },
    /** 修改 */
    async handleUpdate (row) {
      try {
        this.handleReset()

        const res = await getNavigation(row.navigationId)

        const data = res.data

        this.request = data

        this.dialog.open = true

        this.dialog.title = '修改导航'

      } catch (error) {
        console.log(error)
      }
    },
    /** 提交 */
    handleSubmit () {
      this.$refs.form.validate(async valid => {
        if (!valid) {
          return
        }

        try {
          const params = Object.assign({}, { ...this.request })

          await (params.navigationId === undefined ? insertNavigation : updateNavigation)(params)

          this.$message.success(params.navigationId === undefined ? '添加成功' : '修改成功')

          this.dialog.open = false;

          this.getTableDataList()
        } catch (error) {
          console.log(error)
        }
      })
    },
    /** 取消 */
    handleCancel () {
      this.dialog.open = false

      this.handleReset()
    }
  }
}

const searchParams = {
  name: undefined,
  isExternalLink: undefined,
  status: undefined,
  beginDate: undefined,
  endDate: undefined,
  createBy: undefined,
  updateBy: undefined,
  pageIndex: 1,
  pageSize: 10
}

const requestParams = {
  navigationId: undefined,
  name: undefined,
  pagePath: undefined,
  link: undefined,
  isExternalLink: '1',
  sort: 100,
  status: '0'
}

const rules = {
  name: [{ required: true, message: '请输入导航名称', trigger: 'blur' }],
  pagePath: [{ required: true, message: '请输入页面地址', trigger: 'blur' }],
  link: [{ required: true, message: '请输入外链地址', trigger: 'blur' }],
  sort: [{ required: true, message: '请输入排序', trigger: 'blur' }]
}
</script>